/*
 * Skill.java
 *
 * Created on January 6, 2008, 1:49 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class Skill {
    private dbConnection dbcon = new dbConnection();
    private String characterName, skillName;
    private int isEquip;
    private int sLevel;
    /** Creates a new instance of Skill */
    public Skill() {
    }
    
    public Skill(String cname, String sname, int lvl, int ie) {
        setCharacterName(cname);
        setSkillName(sname);
        setIsEquip(ie);
        setSLevel(lvl);
    }
    
    public boolean checkExist(Skill s) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM Skill WHERE characterName = ? AND skillName = ? AND sLevel = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, s.getCharacterName());
            stmt.setString(2, s.getSkillName());
            stmt.setInt(3, s.getSLevel());
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    public void addMember (Skill s){
      						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;
        if (!checkExist(s))
        {
            getDbcon().connect();
            query = "INSERT INTO Skill (skillName, characterName, sLevel, isEquip) VALUES (?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, s.getSkillName());
                stmt.setString(2, s.getCharacterName());
                stmt.setInt(3, s.getSLevel());
                stmt.setInt(4, s.getIsEquip());
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();
        }
    }
    public Vector viewSkills (String name){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Skill> sList;
            
        sList = new Vector<Skill>();
        Iterator it = sList.iterator();
                
        getDbcon().connect();
        query = "SELECT skillName, characterName, sLevel, isEquip FROM Skill WHERE characterName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, name);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Skill s = new Skill();
                s.setSkillName(rs.getString("skillName").trim());
                s.setCharacterName(rs.getString("characterName").trim());
                s.setSLevel(rs.getInt("sLevel"));
                s.isEquip = rs.getInt("isEquip");
                sList.add(s);
              
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return sList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
    
     public void viewSkill (String sname, String cname){
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT skillName, characterName, sLevel, isEquip FROM Skill WHERE characterName = ? AND skillName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, sname);
            stmt.setString (2, cname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                this.setSkillName(rs.getString("skillName").trim());
                this.setCharacterName(rs.getString("characterName").trim());
                this.setSLevel(rs.getInt("sLevel"));
                this.isEquip = rs.getInt("isEquip");
            }
            rs.close(); 					
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
 
        }
    }
     public boolean isHere (int isEq, String cname){
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean bHere = false;
                
        getDbcon().connect();
        query = "SELECT skillName, characterName, sLevel, isEquip FROM Skill WHERE isEquip = ? AND characterName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt (1, isEq);
            stmt.setString (2, cname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                this.setSkillName(rs.getString("skillName").trim());
                this.setCharacterName(rs.getString("characterName").trim());
                this.setSLevel(rs.getInt("sLevel"));
                this.isEquip = rs.getInt("isEquip");
                bHere = true;
            }
            rs.close(); 					
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
            return bHere;
        }
    }
     public Vector viewEquipedSkills (String name){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Skill> sList;
            
        sList = new Vector<Skill>();
        Iterator it = sList.iterator();
                
        getDbcon().connect();
        query = "SELECT skillName, characterName, sLevel, isEquip FROM Skill WHERE characterName = ? AND ";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, name);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Skill s = new Skill();
                s.setSkillName(rs.getString("skillName").trim());
                s.setCharacterName(rs.getString("characterName").trim());
                s.setSLevel(rs.getInt("sLevel"));
                s.isEquip = rs.getInt("isEquip");
                sList.add(s);
              
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return sList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
     public void editIsEquip (String character, String sname, int isEq){
        
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;
        getDbcon().connect();
        query = "UPDATE Skill SET isEquip = ? WHERE characterName = ? AND skillName = ?";
        try 
        {
             
            stmt = getDbcon().con.prepareStatement(query);       
            stmt.setInt(1, isEq);
            stmt.setString(2, character);
            stmt.setString(3, sname);
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
    }

    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getCharacterName() {
        return characterName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }

    public String getSkillName() {
        return skillName;
    }

    public void setSkillName(String skillName) {
        this.skillName = skillName;
    }

    public int getSLevel() {
        return sLevel;
    }

    public void setSLevel(int sLevel) {
        this.sLevel = sLevel;
    }

    public int getIsEquip() {
        return isEquip;
    }

    public void setIsEquip(int isEquip) {
        this.isEquip = isEquip;
    }
}
